//
//  HYGameInfoCommentCell.m
//  
//
//  Created by skyline on 2018/5/16.
//

#import "HYGameInfoCommentCell.h"
NSString * const HYGameInfoCommentCellRefresh = @"HYGameInfoCommentCellRefresh";
NSString * const HYGameInfoCommentCellMore = @"HYGameInfoCommentCellMore";
NSString * const CCNoticeHYGameInfoCommentCellList = @"CCNoticeHYGameInfoCommentCellList";

@interface HYGameInfoCommentCell()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGFloat cellH;
@end
@implementation HYGameInfoCommentCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.tableView.hidden = NO;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        [CCNotice addObs:self sel:@selector(actionForGetList:) name:CCNoticeHYGameInfoCommentCellList obj:nil];
    }
    return self;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForHYGameInfoCommentCellRefresh:)];
        _tableView.mj_footer = [HYRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(actionForHYGameInfoCommentCellMore:)];
        _tableView.separatorStyle = UIAccessibilityTraitNone;
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsZero);
        }];
    }
    return _tableView;
}
- (void)setList:(HYCommentList *)list
{
    if (_list != list)
    {
        _list = list;
        [self.tableView reloadData];
    }
}

- (void)actionForHYGameInfoCommentCellRefresh:(UITableView *)view
{
    [CCNotice post:HYGameInfoCommentCellRefresh obj:nil info:nil];
}
- (void)actionForHYGameInfoCommentCellMore:(UITableView *)view
{
    if (self.list.more)
        [CCNotice post:HYGameInfoCommentCellMore obj:nil info:nil];
    else
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
}
- (void)actionForGetList:(NSNotification *)notify
{
    self.list  = [notify.userInfo valueForKey:@"list"];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"HYInfoCommentCell";
    HYInfoCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[HYInfoCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.commentModel = self.list.list[indexPath.row];
    self.cellH = CGRectGetHeight(cell.bounds);
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellH;
}

@end
